#include "widget.h"
#include "ui_widget.h"

#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 创建3 行
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);
    // 创建3列
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(0);

    // 给3列设置名字
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("语文成绩"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("数学成绩"));

    // 初始化数据
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("80"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("85"));

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("80"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("85"));

    ui->tableWidget->setItem(2,0,new QTableWidgetItem("王五"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("60"));
    ui->tableWidget->setItem(2,2,new QTableWidgetItem("67"));

}


Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_add_row_clicked()
{
    int curRow = ui->tableWidget->rowCount();
     qDebug() << "curRow = " << curRow;
    ui->tableWidget->insertRow(curRow);
}

void Widget::on_pushButton_sub_row_clicked()
{
    int curRow = ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(curRow);
}

void Widget::on_pushButton_add_col_clicked()
{
    int curCol = ui->tableWidget->columnCount();
    qDebug() << "curCol = " << curCol;
    ui->tableWidget->insertColumn(curCol);
    QString name = ui->lineEdit->text();
    ui->tableWidget->setHorizontalHeaderItem(curCol,new QTableWidgetItem(name));
}


void Widget::on_pushButton_sub_row_2_clicked()
{
    int curCol = ui->tableWidget->currentColumn();
    ui->tableWidget->removeColumn(curCol);
}
